# -*- coding: utf-8 -*-

from datetime import datetime
from collections import defaultdict

from settings import db, render

from common import util
status = ["todo", "doing", "done"]


import web


urls = (
  "/(\d+)/list", "kanban_list",
  "/(\d+)/add", "kanban_add",
)

class kanban_list:
    def GET(self, prj_id):
        tasks = [t for t in db.kanban.find({"status":{"$in": status}, "prj_id":prj_id})]
    
        d = defaultdict(list)
        [d[t['status']].append(t) for t in tasks]
        return render("kanban/kanban.html", tasks=d)

class kanban_add:
    @util.jsonresp('id')
    @util.bind(dict, 'name', 'desc', 'priority', 'owner', status='todo', add_time=datetime.now())
    def GET(self, inst, prj_id=0):
        #assert prj_id not null
        inst["prj_id"] = prj_id
        print inst
    
        return str(db.kanban.insert(inst))

app = web.application(urls, locals(), autoreload=True)
